package org.convallaria.system.biz.util;

import cn.hutool.core.bean.BeanUtil;
import org.convallaria.system.biz.entity.SysUser;
import org.convallaria.system.biz.entity.dto.sys.UserCreateDTO;
import org.convallaria.system.biz.entity.dto.sys.UserUpdateDTO;
import org.convallaria.system.biz.entity.dto.sys.UserProfileDTO;
import org.springframework.security.crypto.password.PasswordEncoder;

import java.time.LocalDateTime;

/**
 * 用户操作工具类
 * 
 * @author convallaria
 * @since 1.0.0
 */
public class UserOperationUtil {
    
    /**
     * 构建用户实体（创建）
     * 
     * @param userDTO 用户创建DTO
     * @param passwordEncoder 密码编码器
     * @param tenantId 租户ID
     * @param currentUsername 当前用户名
     * @return 用户实体
     */
    public static SysUser buildUserForCreate(UserCreateDTO userDTO, 
                                           PasswordEncoder passwordEncoder, 
                                           Long tenantId, 
                                           String currentUsername) {
        SysUser user = new SysUser();
        BeanUtil.copyProperties(userDTO, user);

        user.setTenantId(tenantId);
        user.setCreateBy(currentUsername);
        user.setPassword(passwordEncoder.encode(userDTO.getPassword()));

        return user;
    }
    
    /**
     * 构建用户实体（更新）
     * 
     * @param userDTO 用户更新DTO
     * @param currentUsername 当前用户名
     * @return 用户实体
     */
    public static SysUser buildUserForUpdate(UserUpdateDTO userDTO, String currentUsername) {
        SysUser user = new SysUser();

        BeanUtil.copyProperties(userDTO, user);

        user.setUpdateBy(currentUsername);

        return user;
    }
    
    /**
     * 更新用户个人资料
     * 
     * @param user 用户实体
     * @param profileDTO 个人资料DTO
     * @param currentUsername 当前用户名
     */
    public static void updateUserProfile(SysUser user, UserProfileDTO profileDTO, String currentUsername) {
        user.setNickName(profileDTO.getNickName());
        user.setRealName(profileDTO.getRealName());
        user.setEmail(profileDTO.getEmail());
        user.setPhoneNumber(profileDTO.getPhoneNumber());
        user.setGender(profileDTO.getGender());
        user.setBirthday(profileDTO.getBirthday());
        user.setAvatar(profileDTO.getAvatar());
        user.setRemark(profileDTO.getRemark());
        user.setUpdateBy(currentUsername);
        user.setUpdateTime(LocalDateTime.now());
    }
    
    /**
     * 更新用户密码
     * 
     * @param user 用户实体
     * @param newPassword 新密码
     * @param passwordEncoder 密码编码器
     * @param currentUsername 当前用户名
     */
    public static void updateUserPassword(SysUser user, 
                                        String newPassword, 
                                        PasswordEncoder passwordEncoder, 
                                        String currentUsername) {
        user.setPassword(passwordEncoder.encode(newPassword));
        user.setUpdateBy(currentUsername);
        user.setUpdateTime(LocalDateTime.now());
    }
    
    /**
     * 更新用户头像
     * 
     * @param user 用户实体
     * @param avatarUrl 头像URL
     * @param currentUsername 当前用户名
     */
    public static void updateUserAvatar(SysUser user, String avatarUrl, String currentUsername) {
        user.setAvatar(avatarUrl);
        user.setUpdateBy(currentUsername);
        user.setUpdateTime(LocalDateTime.now());
    }
}